package cn.youruan.demo.utils;

import cn.youruan.demo.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

final public class UserContext {

    private static final String USER_IN_SESSION = "USER_IN_SESSION";

    private static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    private static HttpSession getSession() {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    public static void setCurrentUser(User user) {
        //用户登录成功后,将当前用户存入session
        getSession().setAttribute(USER_IN_SESSION,user);
    }

    public  static User getCurrentUser() {
        return (User) getSession().getAttribute(USER_IN_SESSION);
    }

    public static void setCurrentExpressions(List<String> expList) {
        //用户登录成功后,将当前用户存入session
        getSession().setAttribute(EXPRESSION_IN_SESSION,expList);
    }

    public  static List<String> getCurrentExpressions() {

        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }

}
